package com.hs.web;

import com.hs.pojo.Dept;
import com.hs.pojo.Teacher;
import com.hs.service.DeptService;
import com.hs.service.impl.DeptServiceImpl;
import com.hs.util.Page;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/dept/deptServlet")
public class DeptServlet extends HttpServlet {

    DeptService deptService=new DeptServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       String action=req.getParameter("action");
       if("select".equals(action)){
           List<Dept> depts = deptService.selectAll();
           req.setAttribute("depts",depts);
           req.getRequestDispatcher("/pages/dept-list.jsp").forward(req,resp);
       }else if("selectDept".equals(action)){
           String pageNum = req.getParameter("pageNum");
           if (pageNum == null){
               pageNum ="1";
           }
           int pn=Integer.parseInt(pageNum);
           Page<Dept> page = deptService.selectPage(pn);
           //封装page对象 同时跳转页面
           req.setAttribute("page",page);
           req.getRequestDispatcher("/pages/dept-list.jsp").forward(req,resp);
       }else if("toadd".equals(action)){
           List<Dept> depts = deptService.selectAll();
           req.setAttribute("depts",depts);
           req.getRequestDispatcher("/pages/teacher-add.jsp").forward(req,resp);
       }


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
